package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author AIR
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/eduorder/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //1、生产订单的方法
    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId,
                         HttpServletRequest request){
        String orderNo = orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderNo",orderNo);
    }

    //2 根据订单id查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getOrderNo,orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("itemOrder",order);
    }

    //根据课程id和用户id查询订单状态
    @GetMapping("getStatusByCourseId/{courseId}/{memberId}")
    public Boolean getStatusByCourseId(@PathVariable String courseId,
                                 @PathVariable String memberId){
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getCourseId,courseId);
        wrapper.eq(Order::getMemberId,memberId);
        wrapper.eq(Order::getStatus,1); //支付状态，1代表已经支付
        long count = orderService.count(wrapper);
        if (count > 0){
            return true;
        }else {
            return false;
        }
    }



}
